<template>
    <div class="evaluate-container">
        <!-- 标题区域开始 -->
        <div class="title-wrap">
            <div class="left-wrap">
                <i class="iconfont icon-pingjia1"></i>
                <span>我的评价</span>
            </div>

            <!-- 搜索区域开始 -->
            <div class="search-wrap">
                <div class="search-box">
                    <el-input
                        v-model="params.goods_name"
                        style="width: 600px"
                        size="large"
                        placeholder="搜索商品"
                        :prefix-icon="Search"
                        :clearable="true"
                        />
                </div>
                <div class="search-btn">
                    <el-button type="default" size="large" @click="handlerSearchGoodsEvaluate">搜索</el-button>
                </div>
            </div>
            <!-- 订单搜索区域结束 -->
        </div>
        <!-- 标题区域结束 -->


        <!-- 内容区域开始 -->
        <div class="content-wrap">
            <ul v-infinite-scroll="handlerLoadMore" :infinite-scroll-immediate="false" class="infinite-list" style="overflow: auto">

                <!-- 骨架屏区域开始 -->
                <el-skeleton animated :loading="isShowSkeleton">
                    <template #template>
                        <li class="infinite-list-item" v-for="(item, index) in 6" :key="index">
                            <div class="goods-wrap">
                                <div class="info">
                                    <el-skeleton-item style="width: 200px; height: 30px;"/>
                                    <el-skeleton-item style="width: 400px; height: 20px;margin: 10px 0;"/>
                                    <el-skeleton-item style="width: 800px; height: 20px;margin: 10px 0;"/>
                                    <el-skeleton-item style="width: 800px; height: 20px;margin: 10px 0;"/>

                                    <div class="evaluate-image">
                                        <el-skeleton-item style="width: 80px; height: 80px;margin: 10px" v-for="(item, index) in 3" :key="index"/>
                                    </div>
                                </div>
                            </div>
                        </li>
                    </template>
                </el-skeleton>
                <!-- 骨架屏区域结束 -->
                <li v-for="(item, i) in goodsEvaluateList" :key="i" class="infinite-list-item">
                    <div class="goods-wrap">
                        <!-- <div class="cover">
                            <img :src="global.FILE_DOMAIN + item.image_url" alt="">
                        </div> -->

                        <div class="info">
                            <span class="sku">{{item.goods_sku_text}}</span>
                            <span class="name">{{item.goods_name}}</span>
                        </div>
                    </div>

                    <div class="evaluate-wrap">
                        <div class="evaluate-content">
                            {{item.contents}}
                        </div>

                        <div class="evaluate-image" v-if="item.image_url_items">
                            <div class="evaluate-image-item" v-for="(item, index) in item.image_url_items" :key="index">
                                <img :src="global.FILE_DOMAIN + item" alt="">
                            </div>
                        </div>
                    </div>
                </li>

                <TridentEmpty v-if="goodsEvaluateList.length == 0"/>
            </ul>
        </div>

    </div>
</template>

<script setup>
import { ref, onMounted, inject, reactive } from 'vue'
import { Search } from '@element-plus/icons-vue'
import { useUserStore } from "@/stores/modules/user"
import { reqGoodsCommentListAPI } from '@/apis/goodsEvaluate'



// ------------------- 定义全局变量开始 -------------------

// 获取全局对象
const global = inject('global')

// 用户存储
const userStore = useUserStore()

const pageTotal = ref(0)

// 请求参数
const params = reactive({
    uid: userStore.profile.user.uid,
    page: 1,
    page_size: 10
})

// 定义骨架屏变量
const isShowSkeleton = ref(false)
// ------------------- 定义全局变量结束 -------------------



// --------------------- 加载页面开始 ---------------------
onMounted( async () => {
    await getGoodsEvaluateList()
})
// --------------------- 加载页面结束 ---------------------



// ------------------- 加载更多数据开始 -------------------
const handlerLoadMore = async () => {
    params.page++
    if (params.page > 1 && params.page <= pageTotal.value) {
        await getGoodsEvaluateList()
    }
}
// ------------------- 加载更多数据结束 -------------------



// --------------------- 搜索评价开始 ---------------------
const handlerSearchGoodsEvaluate = async () => {
    params.page = 1
	goodsEvaluateList.value = []
    await getGoodsEvaluateList()
}
// --------------------- 搜索评价结束 ---------------------



// ------------------- 获取评价数据开始 -------------------
// 评价数据
const goodsEvaluateList = ref([])

// 总条数
const total = ref(0)

// 请求商品评价数据
const getGoodsEvaluateList = async () => {
    isShowSkeleton.value = true
    const result = await reqGoodsCommentListAPI(params)
    if (result.code == 200) {
        let goodsEvaluates = result.data.goods_comments != null ? result.data.goods_comments : []
        total.value = result.data.total
        pageTotal.value = result.data.page_total
        goodsEvaluateList.value.forEach((item) => {
            item.giveLike = false
        })

        goodsEvaluateList.value.push(...goodsEvaluates)
    }

    isShowSkeleton.value = false
}
// ------------------- 获取评价数据结束 -------------------
</script>

<style scoped lang="scss">
.evaluate-container {
    background-color: #fff;
    padding: 0 15px 15px 15px;
    .title-wrap {
        padding: 15px 0;
        font-size: 18px;
        border-bottom: 1px solid #f5f5f5;
        color: $main-color;
        display: flex;
        justify-content: space-between;
        align-items: center;
        .left-wrap {
            display: flex;
            align-items: center;
            .iconfont {
                font-size: 24px;
                margin-right: 5px;
            }
        }
        .search-wrap {
            display: flex;
            .search-box {
                margin-right: 10px;
            }
        }
    }

    .content-wrap {
        padding: 10px 0;
        ::-webkit-scrollbar {
            display: none;
        }
        .infinite-list {
            height: 410px;
            padding: 0;
            margin: 0;
            list-style: none;
            .infinite-list-item {
                padding: 20px 0;
                border-bottom: 1px solid #f5f5f5;
                &:last-child {
                    border-bottom: none;
                }
                .goods-wrap {
                    display: flex;
                    align-items: center;
                    .cover {
                        width: 50px;
                        height: 50px;
                        img {
                            width: 100%;
                            height: 100%;
                        }
                    }
                    .info {
                        display: flex;
                        flex-direction: column;
                        padding-left: 10px;
                        .sku {
                            padding-bottom: 10px;
                            font-weight: bold;
                        }
                        .name {
                            font-size: 14px;
                            color: #999;
                        }
                    }
                }

                .evaluate-wrap {
                    padding: 20px 10px;
                    .evaluate-content {
                        color: #666;
                        line-height: 20px;
                    }
                    .evaluate-image {
                        display: flex;
                        padding-top: 10px;
                        .evaluate-image-item {
                            padding-right: 10px;
                            width: 80px;
                            height: 80px;
                            &:last-child {
                                padding-right: 0;
                            }
                            img {
                                width: 100%;
                                height: 100%;
                            }
                        }
                    }
                }
            }
        }
    }
}
</style>